package com.tools.cleanmaster.utils;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import com.tools.cleanmaster.common.thread.ThreadPool;

import java.io.Closeable;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Locale;

/**
 * desc:
 * date: 2018/6/12
 * author: ancun
 */

public class Utilities {
    static Context sApplicationContext = null;
    static String sAppliationName = null;
    static String sMainProcessName = null;

    private static Boolean sIsLargeScreen = null;

    public static boolean ensureDirectory(String dirPath) {
        try {
            File dir = new File(dirPath);
            if ((dir.exists()) && (!dir.isDirectory())) {
                dir.delete();
            }
            if (!dir.exists()) {
                dir.mkdirs();
            }
        } catch (Exception e) {
            Log.e("Utilities", "ensureDirectory - " + e);
            return false;
        }
        return true;
    }

    public static void initEnvironment(Context ctx, String name, String mainProcessName) {
        sApplicationContext = ctx;
        sAppliationName = name;
        sMainProcessName = mainProcessName;
    }

    public static Context getApplicationContext() {
        if (sApplicationContext == null) {
            throw new IllegalStateException("Common library is used before initialize!");
        }

        return sApplicationContext;
    }

    public static String getApplicationName() {
        return sAppliationName;
    }

    public static void runBackgroundInitializer() {
        ThreadPool.runOnPool(new Runnable() {
            public void run() {
                DevicesUtils.getPhysicalMemoryKBs();
            }
        });
    }

    public static void setVmMemoryPolicy() {
        try {
            ClassLoader cl = ClassLoader.getSystemClassLoader();

            Class clsVMRuntime = cl.loadClass("dalvik.system.VMRuntime");

            Method methodGetRuntime = clsVMRuntime.getMethod("getRuntime");
            Method methodSetMinimumHeapSize = clsVMRuntime.getMethod("setMinimumHeapSize", Long.TYPE);

            Method methodSetTargetHeapUtilization = clsVMRuntime.getMethod("setTargetHeapUtilization", Float.TYPE);

            int minHeap = Build.VERSION.SDK_INT <= 10 ? 8 : 12;
            Object objRuntime = methodGetRuntime.invoke(null, (Object[]) null);
            methodSetMinimumHeapSize.invoke(objRuntime, Integer.valueOf(minHeap * 1024 * 1024));
            methodSetTargetHeapUtilization.invoke(objRuntime, Float.valueOf(0.85F));

        } catch (Exception e) {

        }
    }

    public static boolean isSDCardMounted() {
        return Environment.getExternalStorageState().equals("mounted");
    }

    public static void silentlyClose(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {
            }
        }
    }

    public static void silentlyClose(Cursor c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {
            }
        }
    }

    public static void silentlyClose(SQLiteDatabase c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {
            }
        }
    }

    public static String getCurrentLanguage() {
        return Locale.getDefault().getLanguage();
    }

    public static boolean isLargeScreen() {
        if (sIsLargeScreen == null) {
            Resources resources = getApplicationContext().getResources();
            int screenSize = resources.getConfiguration().screenLayout & 0xF;

            sIsLargeScreen = Boolean.valueOf((screenSize == 3) || (screenSize == 4));
        }

        return sIsLargeScreen.booleanValue();
    }

    public static void printArray(String name, int[] array) {
        StringBuilder sb = new StringBuilder();
        sb.append(name == null ? "" : name).append(":[");
        for (int i = 0; i < array.length - 1; i++) {
            sb.append(array[i]).append(",");
        }
        sb.append(array[(array.length - 1)]).append("]");
    }

    public static void printArray(String name, float[] array) {
        StringBuilder sb = new StringBuilder();
        sb.append(name == null ? "" : name).append(":[");
        for (int i = 0; i < array.length - 1; i++) {
            sb.append(array[i]).append(",");
        }
        sb.append(array[(array.length - 1)]).append("]");
    }

    public static void printArray(String name, Object[] array) {
        StringBuilder sb = new StringBuilder();
        sb.append(name == null ? "" : name).append(":[");
        for (int i = 0; i < array.length - 1; i++) {
            sb.append(array[i]).append(",");
        }
        sb.append(array[(array.length - 1)]).append("]");
    }

    public static boolean supportVelocityWithParam() {
        return Build.VERSION.SDK_INT >= 8;
    }
}
